package cn.zynworld.hnister.chats.rest;

import cn.zynworld.hnister.chats.convertor.ChatMessageConvertor;
import cn.zynworld.hnister.chats.dto.ChatMessageDTO;
import cn.zynworld.hnister.chats.manager.MqttManager;
import cn.zynworld.hnister.chats.service.ChatService;
import cn.zynworld.hnister.chats.vo.ChatMessageVO;
import cn.zynworld.hnister.common.utils.AccountUtils;
import cn.zynworld.hnister.common.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;

/**
 * @auther Buynow Zhao
 * @create 2018/5/6
 */
@RestController
@RequestMapping(path = "rest")
public class ChatRest {

	@Autowired
	private ChatService chatService;

	@PostMapping(path = "df/chatMessage")
	public ResultBean chatmsg(@RequestBody ChatMessageVO chatMessageVO) {
		String username = AccountUtils.getUsername();
		if (StringUtils.isEmpty(username)) {
			return ResultBean.fail("账户异常");
		}
		ChatMessageDTO chatMessageDTO = ChatMessageConvertor.vo2dto(chatMessageVO);
		chatMessageDTO.setUsername(username);
		chatMessageDTO.setSendTime(new Date().getTime());

		chatService.publishChatMessage(chatMessageDTO);
		return ResultBean.success();
	}

}
